package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Jt
 * @date 2021/4/22 21:00
 */
@Service
@Slf4j
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Resource
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
//        1. 查询行为实体
        ApBehaviorEntry byUserIdOrEquipmentId = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(), null);
        if (byUserIdOrEquipmentId == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
//        保存关注行为
        ApFollowBehavior behavior = new ApFollowBehavior();
        behavior.setArticleId(dto.getArticleId());
        behavior.setCreatedTime(new Date());
        behavior.setEntryId(byUserIdOrEquipmentId.getId());
        behavior.setFollowId(dto.getFollowId());
        save(behavior);
        return ResponseResult.okResult();
    }

}
